package mf.words.navigation;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 * Graphical interface for navigating through the text. 
 */
public class WordFrame extends JFrame implements ActionListener, KeyListener {
	
	private static final long serialVersionUID = 8885954886155324139L;

	int last_direction = KeyEvent.VK_RIGHT;
	
	//data model
	WordModel data;
	
	//information label
	JLabel lblInfo = new JLabel("<html><body>Navigate with left and right arrow keys through the file.<br /><span>&nbsp;</span></html>");
	//debug label
	JLabel lblDebug = new JLabel("<html><body></body></html>");
	//here the output is presented
	JTextArea taOutput = new JTextArea();
	//scroll pane for longer texts
	JScrollPane spOutput;
	
	//menu bar for opening new file, saving and quit, ...
	JMenuBar mnbMenu = new JMenuBar();
	JMenu mnuMain = new JMenu("Unpair");
	JMenuItem mniOpen = new JMenuItem("Open", KeyEvent.VK_O);
	JMenuItem mniClose= new JMenuItem("Quit", KeyEvent.VK_Q);
	
	/** Generates a new frame with given title and model. */
	public WordFrame(String title, WordModel model) {
		//set model
		data = model;
		
		//some general settings
		setTitle(title);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(640, 190 + (data.debugMode() ? 40 : 0));
		setResizable(false);
		setLayout(null);
		
		//add key Listener 
		addKeyListener(this);
		
		//set info labels
		lblInfo.setLocation(0, 0);
		lblInfo.setSize(640, 40);
		
		//set debug labels
		lblDebug.setLocation(0,140);
		lblDebug.setSize(640, 40);
		lblDebug.setText(data.getDebugText());
		
		//set output text area
		taOutput.setLineWrap(true);
		taOutput.setEnabled(false);
		taOutput.setBackground(new Color(238, 238, 238));
		taOutput.setDisabledTextColor(Color.BLACK);
		taOutput.setText(data.getTextSymbol());

		//add scroll pane
		spOutput = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		spOutput.setViewportView(taOutput);
		spOutput.setSize(getWidth()-6,100);
		spOutput.setLocation(0, 40);
		
		//assemble menu
		mniOpen.addActionListener(this);
		mniOpen.setActionCommand("open");
		
		mniClose.addActionListener(this);
		mniClose.setActionCommand("close");
		
		mnuMain.add(mniOpen);
		mnuMain.add(mniClose);
		mnbMenu.add(mnuMain);

		//put all together
		add(lblInfo);
		add(spOutput);
		add(lblDebug);
		setJMenuBar(mnbMenu);
	}

	/** Menu actions.	*/
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("open")) {
			try {
				JFileChooser jfc = new JFileChooser(new File("."));
				jfc.setDialogTitle("Select the file you want to unpair...");
				
				if(jfc.showOpenDialog(null) != JFileChooser.APPROVE_OPTION)
					System.exit(0);
			
				data = new WordModel(jfc.getSelectedFile(), data.debugMode());
			} catch (IOException ioe) {}
			
			taOutput.setText(this.data.getTextSymbol());
		}
		if(e.getActionCommand().equals("close"))
			System.exit(0);
	}

	/** Key actions. */
	public void keyPressed(KeyEvent e) {
		switch(e.getKeyCode()) {
		default:
			return;
		case KeyEvent.VK_LEFT:
			data.navigateLeft();
			break;
		case KeyEvent.VK_RIGHT:
			data.navigateRight();
			break;
		}
		
		if(!data.atBorder()) {
			if(e.getKeyCode() == last_direction)
				taOutput.setText(taOutput.getText() + data.getTextSymbol());
			else
				taOutput.setText(taOutput.getText().substring(taOutput.getText().length()) + data.getTextSymbol());
		
			last_direction = e.getKeyCode();
		}
		
		lblInfo.setText(data.getInfoText());
		lblDebug.setText(data.getDebugText());
	}

	public void keyReleased(KeyEvent e) {}
	public void keyTyped(KeyEvent e) {}

}
